/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Model.InitialData;
import java.awt.HeadlessException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author William
 */
public class InitialDataPanel extends javax.swing.JPanel {

    /**
     * Creates new form InitialDataPanel
     */
    private InitialData initialData = Application.initialData;

    public InitialDataPanel() {
        initComponents();
        firstData();
        doTableModels();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        objectiveFunctionsTable = new javax.swing.JTable();
        saveButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        constraintsTable = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setMaximumSize(new java.awt.Dimension(1366, 788));
        setMinimumSize(new java.awt.Dimension(600, 350));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Ingreso de Datos.");

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Funciones objetivo:");

        objectiveFunctionsTable.setColumnSelectionAllowed(true);
        objectiveFunctionsTable.setGridColor(new java.awt.Color(102, 102, 102));
        objectiveFunctionsTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                objectiveFunctionsTableKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(objectiveFunctionsTable);
        objectiveFunctionsTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        saveButton.setText("Guardar");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        constraintsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        constraintsTable.setGridColor(new java.awt.Color(102, 102, 102));
        constraintsTable.setRowSelectionAllowed(false);
        constraintsTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                constraintsTableKeyTyped(evt);
            }
        });
        jScrollPane2.setViewportView(constraintsTable);

        jLabel3.setBackground(new java.awt.Color(255, 255, 255));
        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Restricciones:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                .addGap(10, 10, 10)
                .addComponent(saveButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        double A[][] = new double[initialData.getNumberObjetives()][initialData.getNumberVariables()];
        double B[][] = new double[initialData.getNumberRestrictions()][initialData.getNumberVariables()];
        double C[] = new double[initialData.getNumberRestrictions()];
        try {
            for (int i = 0; i < initialData.getNumberObjetives(); i++) {
                for (int j = 0; j < initialData.getNumberVariables(); j++) {
                    A[i][j] = Double.parseDouble(objectiveFunctionsTable.getValueAt(i, j).toString());
                }
            }
            for (int i = 0; i < initialData.getNumberRestrictions(); i++) {
                for (int j = 0; j < initialData.getNumberVariables(); j++) {
                    B[i][j] = Double.parseDouble(constraintsTable.getValueAt(i, j).toString());
                }
            }
            for (int i = 0; i < initialData.getNumberRestrictions(); i++) {
                C[i] = Double.parseDouble(constraintsTable.getValueAt(i, initialData.getNumberVariables()).toString());
            }
            initialData.setObjectiveData(A);
            initialData.setRestrictionData(B);
            initialData.setLdData(C);
            initialData.setSaved(true);
            JOptionPane.showMessageDialog(this, "Guardado.", "Informatción.", JOptionPane.INFORMATION_MESSAGE);
        } catch (NumberFormatException nfe) {
            JOptionPane.showMessageDialog(this, "por favor revise que los valores de las tablas se encuentran llenos.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void objectiveFunctionsTableKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_objectiveFunctionsTableKeyTyped
        char k = evt.getKeyChar();
        if (!Character.isDigit(k) && (k != '-')&&(k != '.')) {
            evt.consume();
        }
    }//GEN-LAST:event_objectiveFunctionsTableKeyTyped

    private void constraintsTableKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_constraintsTableKeyTyped
        char k = evt.getKeyChar();
        if (!Character.isDigit(k) && (k != '-')&&(k != '.')) {
            evt.consume();
        }
    }//GEN-LAST:event_constraintsTableKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable constraintsTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable objectiveFunctionsTable;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

    private void firstData() {
        boolean flag = true;
        while (flag) {
            try {
                initialData.setNumberVariables(Integer.parseInt(JOptionPane.showInputDialog(this, "Ingrese el número de variables máximo de las funciones objetivo.", "Variables de las funciones objetivo.", JOptionPane.QUESTION_MESSAGE)));
                initialData.setNumberObjetives(Integer.parseInt(JOptionPane.showInputDialog(this, "Ingrese el número de funciones objetivo.", "Funciones Objetivo.", JOptionPane.QUESTION_MESSAGE)));
                initialData.setNumberRestrictions(Integer.parseInt(JOptionPane.showInputDialog(this, "Ingrese el número de restricciones.", "Restricciones.", JOptionPane.QUESTION_MESSAGE)));
                flag = false;
            } catch (NumberFormatException nfe) {
                flag = true;
                JOptionPane.showMessageDialog(this, "Ingrese números por favor.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void doTableModels() {
        DefaultTableModel dtm = new DefaultTableModel();
        Object vector[] = new Object[initialData.getNumberVariables()];
        for (int i = 0; i < initialData.getNumberVariables(); i++) {
            dtm.addColumn("x" + (i + 1));
            vector[i] = 0;
        }

        for (int i = 0; i < initialData.getNumberObjetives(); i++) {
            dtm.addRow(vector);
        }
        DefaultTableModel dtm2 = new DefaultTableModel();
        Object vectorB[] = new Object[initialData.getNumberVariables()];
        for (int i = 0; i < initialData.getNumberVariables(); i++) {
            dtm2.addColumn("x" + (i + 1));
            vectorB[i] = 0;
        }
        for (int i = 0; i < initialData.getNumberRestrictions(); i++) {
            dtm2.addRow(vectorB);
        }
        Object vectorC[] = new Object[initialData.getNumberRestrictions()];
        for (int i = 0; i < initialData.getNumberRestrictions(); i++) {
            vectorC[i] = 0;
        }
        dtm2.addColumn("LD", vectorC);

        objectiveFunctionsTable.setModel(dtm);
        constraintsTable.setModel(dtm2);


    }
}
